package com.taotao.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.CommonResult;
import com.taotao.common.EasyUIDataGridResult;
import com.taotao.common.ResultGenerator;
import com.taotao.mapper.TbUserMapper;
import com.taotao.pojo.TbUser;
import com.taotao.pojo.TbUserExample;
import com.taotao.pojo.TbUserExample.Criteria;
import com.taotao.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private TbUserMapper tbUserMapper;

	@Value("${shiroehcache.hashIterations}")
	private int hashIterations;

	@Value("${shiroehcache.algorithmName}")
	private String algorithmName;

	@Override
	public TbUser login(TbUser user) {
		TbUserExample tbUserExample = new TbUserExample();
		Criteria createCriteria = tbUserExample.createCriteria();
		if (user != null) {
			String username = user.getUsername();
			createCriteria.andUsernameEqualTo(username);
			List<TbUser> selectByExample = tbUserMapper.selectByExample(tbUserExample);
			if (selectByExample != null && !selectByExample.isEmpty()) {
				return selectByExample.get(0);
			}
		}
		return null;
	}

	@Override
	public TbUser getUserByOtherWay(String otherWayValue) {
		TbUserExample tbUserExample = new TbUserExample();
		if (otherWayValue != null && !otherWayValue.isEmpty()) {
			// 鏉′欢1 鍖归厤鐢ㄦ埛鍚�
			Criteria createCriteria = tbUserExample.createCriteria();
			createCriteria.andUsernameEqualTo(otherWayValue);
			// 鏉′欢2 鍖归厤鎵嬫満鍙风爜
			Criteria createCriteria2 = tbUserExample.or();
			createCriteria2.andPhoneEqualTo(otherWayValue);
			// 鏉′欢3 鍖归厤閭
			Criteria createCriteria3 = tbUserExample.or();
			createCriteria3.andPhoneEqualTo(otherWayValue);
			List<TbUser> selectByExample = tbUserMapper.selectByExample(tbUserExample);
			if (selectByExample != null && !selectByExample.isEmpty()) {
				return selectByExample.get(0);
			}
		}
		return null;
	}

	@Override
	public TbUser saveUser(TbUser _user) {
		if (_user != null) {
			TbUser user = _user.cloneUser();
			String salt = new SecureRandomNumberGenerator().nextBytes().toHex();
			user.setSalt(salt);
			String credentialsSalt = user.getCredentialsSalt();
			SimpleHash hash = new SimpleHash(algorithmName, user.getPassword(), credentialsSalt, hashIterations);
			String encodedPassword = hash.toHex();
			user.setPassword(encodedPassword);
			Date created = new Date();
			user.setCreated(created);
			user.setUpdated(created);
			int insertSelective = tbUserMapper.insertSelective(user);
			if (insertSelective > 0) {
				return _user;
			}
		}
		return null;
	}

	@Override
	public EasyUIDataGridResult getUserList(int page, int rows) {
		// 开始分页
		PageHelper.startPage(page, rows);
		// 条件构造器
		TbUserExample tbUserExample = new TbUserExample();
		List<TbUser> list = tbUserMapper.selectByExample(tbUserExample);
		// 构造PageInfo对象
		PageInfo<TbUser> pageInfo = new PageInfo<TbUser>(list);
		// 构造前端gird需要的EasyUIDataGridResult对象
		EasyUIDataGridResult result = new EasyUIDataGridResult();
		result.setRows(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}

	@Override
	public CommonResult editUserSome(Long id, TbUser user) {
		TbUserExample tbUserExample = new TbUserExample();
		Criteria createCriteria = tbUserExample.createCriteria();
		createCriteria.andIdEqualTo(id);
		int result = tbUserMapper.updateByExampleSelective(user, tbUserExample);
		if(result >0) {
			return ResultGenerator.genSuccessResult(user);
		}
		return ResultGenerator.genFailResult("更新用户信息失败");
	}

}
